package com.jeeplus.modules.guide.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.PageBean;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guide.entity.*;
import com.jeeplus.modules.guide.mapper.MobileEvaluateMapper;
import com.jeeplus.modules.sys.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MobileEvaluateService {
    private static final Logger log = LoggerFactory.getLogger(MobileEvaluateService.class);
    private final MobileEvaluateMapper mobileEvaluatemapper;

    public PageBean<MobileEvaluate> getMobileEvaluateList(Map<String,Object> map){
        PageBean<MobileEvaluate> pageBean = new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));
        PageHelper.orderBy(" evaluateLevel asc");
        List<MobileEvaluate> list = mobileEvaluatemapper.getMobileEvaluateList(map);
        PageInfo<MobileEvaluate> pageInfo = new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    public PageBean<MobileEvaluateHistoryValue> findEvaluateList(Map<String,Object> map){
        PageBean<MobileEvaluateHistoryValue> pageBean = new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));
        PageHelper.orderBy(" doTime desc,doStatus asc");
        List<MobileEvaluateHistoryValue> list = mobileEvaluatemapper.findEvaluateList(map);
        PageInfo<MobileEvaluateHistoryValue> pageInfo = new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    public List<MobileEvaluateValue> getEvaluateTree(){
        return mobileEvaluatemapper.getEvaluateTree();
    }


    public MobileEvaluate getEvaluateValue(String id) {
        return mobileEvaluatemapper.getEvaluateValue(id);
    }
    public HashMap<String,Object> getEvaluateValueHistory(String id) {
        return mobileEvaluatemapper.getEvaluateValueHistory(id);
    }


    @Transactional(readOnly = false)
    public void delEvaluate(String id) {
        mobileEvaluatemapper.delEvaluate(id);
    }


    @Transactional(readOnly = false)
    public void updateEvaluate(MobileEvaluate me) {
        mobileEvaluatemapper.updateEvaluate(me);
    }

    @Transactional(readOnly = false)
    public void updateEvaluateHistory(String id,int doStatus) {
        mobileEvaluatemapper.updateEvaluateHistory(id,doStatus);
    }


    @Transactional(readOnly = false)
    public void insertEvaluate(MobileEvaluate me) {
        me.setId(UUID.randomUUID().toString());
        mobileEvaluatemapper.insertEvaluate(me);
    }

    public List<MobileEvaluate> getEvaluateList(){
        return mobileEvaluatemapper.getEvaluateList();
    }

    @Transactional(readOnly = false)
    public void insertEvaluateValue(MobileEvaluateHistory me){
        String userId = "";
        if(null != UserUtils.getUser()){
            userId = UserUtils.getUser().getId();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String id = UUID.randomUUID().toString();
        int result = mobileEvaluatemapper.insertEvaluateValue(id,me.getEvaluateLevel(), userId,sdf.format(new Date()),me.getIsAnonymous(),me.getOid(),me.getRemark(),me.getIsThing());
        if(result >= 1){
            if(StringUtils.isNotBlank(me.getIds())){
                mobileEvaluatemapper.insertEvaluateLevel(id,Arrays.asList(me.getIds().split(",")));
            }
        }
    }

    public PageBean<MobileUserListEvaluate> userListEvaluate(Map<String,Object> map){
        PageBean<MobileUserListEvaluate> pageBean = new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));
        PageHelper.orderBy(" doTime desc");
        List<MobileUserListEvaluate> list = mobileEvaluatemapper.userListEvaluate(map);
        PageInfo<MobileUserListEvaluate> pageInfo = new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    public List<MobileCountEvaluate> userCountEvaluate(String oid,int isThing){
        return mobileEvaluatemapper.userCountEvaluate(oid,isThing);
    }

    public List<MobileCountLevel> userCountByLevel(String oid,int isThing){
        return mobileEvaluatemapper.userCountByLevel(oid,isThing);
    }

}
